<?php 
/**
 * 货品管理控制器
 */
class GoodsListController extends AuthController{
	private $model;
	public function __init(){
		parent::__init();
		$this->model = K('GoodsList');
	}
	
	/**
	 * 加载首页模板
	 */
	public function index(){
		//查询要分配的数据
		//对相同规格的数据进行分组
		$gid = Q('get.gid');
		$datagroup = M()->join("__goods_attr__ as ga JOIN __type_attr__ as ta ON ga.type_attr_taid=ta.taid")->where("ga.class=0 AND ga.goods_gid={$gid}")->group('taname')->all();
		$this->assign('datagroup',$datagroup);
		//获取货品值
		$glistData = $this->model->where("goods_gid={$gid}")->all();
		foreach ($glistData as $k => $v) {
			//查询combinespec对应的值
			$glistData[$k]['glvalue'] = K('GoodsAttr')->where("gaid IN (" . $v['combinespec'] . ")")->getField("gvalue",true);
		}
		$this->assign('glistData',$glistData);
		$this->display();
	}
	
	
	/**
	 * 加载添加属性
	 */
	public function add(){
		if(IS_POST){
			if(!$this->model->addglist()) $this->error($this->model->error);
			$this->success('添加成功');
		}
		$gid = Q('get.gid',0,'intval');
		//分配需要添加的数据
		$datagroup = M()->join("__goods_attr__ as ga JOIN __type_attr__ as ta ON ga.type_attr_taid=ta.taid")->where("ga.class=0 AND ga.goods_gid={$gid}")->group('taname')->all();
		//获得规格的taname值作为查询条件，查询taname一类的数据在进行数组从组
		foreach ($datagroup as $k => $v) {
			$type = $v['taname'];
			$data = M()->join("__goods_attr__ as ga JOIN __type_attr__ as ta ON ga.type_attr_taid=ta.taid")->where("ga.class=0 AND ta.taname='{$type}' AND ga.goods_gid={$gid}")->all();
			foreach ($data as $kk => $vv) {
				$gaid = $vv['gaid'];
				$datagroup[$k]['gvalues'][$gaid] = $vv['gvalue'];
			}
		}

		$this->assign('datagroup',$datagroup);
		$this->display();
		
	}
	
	/**
	 * 编辑属性
	 */
	public function edit(){
		$glid = Q("get.glid");	
		if(IS_POST){
			if(!$this->model->edit($glid)) $this->error($this->model->error);
			$this->success('编辑成功');
		}
		//沿用添加模板中的数据
		//分配需要添加的数据
		$datagroup = M()->join("__goods_attr__ as ga JOIN __type_attr__ as ta ON ga.type_attr_taid=ta.taid")->where("ga.class=0")->group('taname')->all();
		//获得规格的taname值作为查询条件，查询taname一类的数据在进行数组从组
		foreach ($datagroup as $k => $v) {
			$type = $v['taname'];
			$data = M()->join("__goods_attr__ as ga JOIN __type_attr__ as ta ON ga.type_attr_taid=ta.taid")->where("ga.class=0 AND ta.taname='{$type}'")->all();
			foreach ($data as $kk => $vv) {
				$gaid = $vv['gaid'];
				$datagroup[$k]['gvalues'][$gaid] = $vv['gvalue'];
			}
		}
		$this->assign('datagroup',$datagroup);
		
		//分配旧数据
		$oldData = $this->model->where("glid={$glid}")->find();
		//combine转化为数组
		$oldData['combinespec'] = explode(',', $oldData['combinespec']);
		$this->assign('oldData',$oldData);
		$this->display();
		
	}
	
	/**
	 * 删除货品
	 */
	public function delete(){
		$glid = Q('get.glid');
		$gid = Q('get.gid');
		$this->model->where("glid={$glid}")->delete();
		//重新统计更新商品库存
		$stockCount = $this->model->where("goods_gid={$gid}")->sum('glstock');
		$data = array(
			'gid'   => $gid,
			'stock' => $stockCount
		);
		K('Goods')->update($data);
		$this->success('删除成功');
		
		
	}




	
	
	
	
}























 ?>